/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.TaiKhoanWebSite_DTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class TaiKhoanWebSite_DAO {
      
    public static ArrayList<TaiKhoanWebSite_DTO> GetALL()
    {
        ArrayList<TaiKhoanWebSite_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from TaiKhoanWebSite" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                TaiKhoanWebSite_DTO TaiKhoan = new TaiKhoanWebSite_DTO();
                TaiKhoan.setID(rs.getInt("ID"));
                TaiKhoan.setTenTK(rs.getString("TenTK"));
                TaiKhoan.setMatKhau(rs.getString("MatKhau"));
                TaiKhoan.setGhiChu(rs.getString("GhiChu"));
                TaiKhoan.setActive(rs.getBoolean("Active"));
                TaiKhoan.setCMND(rs.getString("CMND"));
                ds.add(TaiKhoan);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(TaiKhoanWebSite_DTO TaiKhoan)
    {
        boolean flag = false;
        try {
            String sql = "Insert TaiKhoanWebSite(TenTK, MauTK, GhiChu, Active, CMND) Values('"+TaiKhoan.getTenTK()+"','"+TaiKhoan.getMatKhau()+"','"+TaiKhoan.getGhiChu()+"',"+TaiKhoan.isActive()+"','"+TaiKhoan.getCMND()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(TaiKhoanWebSite_DTO TaiKhoan)
    {
        boolean flag = false;
        try {
            String sql = "Update TaiKhoanWebSite Set TenTK = '"+ TaiKhoan.getTenTK() +"', MatKhau = '"+TaiKhoan.getMatKhau()+"',GhiChu = '"+TaiKhoan.getGhiChu()+"', Active = "+TaiKhoan.isActive()+", CMND = '"+ TaiKhoan.getCMND()+ "' where ID = "+ TaiKhoan.getID();
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(int maID)
    {
        boolean flag = false;
        try {
            String sql = "Delete from TaiKhoanWebSite where ID = "+ maID;
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
}
